package com.xhwl.logistics.strategy.impl;

import com.xhwl.logistics.bo.VehicleCarportBO;
import com.xhwl.logistics.bo.VehicleParkingLotSecondBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.manipulate.VehicleSyncBO;
import com.xhwl.logistics.converter.mapstruct.CentBiConverter;
import com.xhwl.logistics.converter.mapstruct.VehicleAccessRecordConverter;
import com.xhwl.logistics.converter.mapstruct.VehicleCarportConverter;
import com.xhwl.logistics.converter.mapstruct.VehiclePayRecordConverter;
import com.xhwl.logistics.converter.mapstruct.VehicleSyncConverter;
import com.xhwl.logistics.dto.http.CarportJieShunDTO;
import com.xhwl.logistics.dto.http.ParkingLotInfoJieShunDTO;
import com.xhwl.logistics.service.IVehicleAccessRecordService;
import com.xhwl.logistics.service.IVehiclePayRecordService;
import com.xhwl.logistics.service.cent.IBIVehicleService;
import com.xhwl.logistics.service.impl.VehicleParkingLotServiceImpl;
import com.xhwl.logistics.service.impl.http.JieShunHttpServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Slf4j
//@Service
public class VehicleParkingLotJieShunHandlerImpl extends AbstractVehicleParkingLotHandlerImpl implements IVehicleParkingLotHandler {

    @Autowired
    private JieShunHttpServiceImpl jieShunHttpService;

    @Autowired
    private VehicleParkingLotServiceImpl vehicleParkingLotService;

    @Autowired
    private IVehicleAccessRecordService vehicleAccessRecordService;

    @Autowired
    private IVehiclePayRecordService vehiclePayRecordService;

    @Autowired
    private IBIVehicleService biVehicleService;

    @Autowired
    private CentBiConverter centBiConverter;

    @Autowired
    private VehicleAccessRecordConverter vehicleAccessRecordConverter;

    @Autowired
    private VehiclePayRecordConverter vehiclePayRecordConverter;

    @Autowired
    private VehicleSyncConverter vehicleSyncConverter;

    @Autowired
    private VehicleCarportConverter vehicleCarportConverter;

    @Override
    public void sync(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, VehicleSyncBO vehicleSyncBO) {
        log.info("JieShun Sync Start!");
        log.info("VehicleIntegratedConfigBO: " + vehicleIntegratedConfigBO);

        Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();
        if (vehicleParkingLotService.judgeExistParkingLot(projectId)) {
            log.info("JieShun Sync Already!");
            return;
        }

        ParkingLotInfoJieShunDTO parkingLotInfoJieShunDTO = jieShunHttpService.getParkingLotInfo(vehicleIntegratedConfigBO);
        vehicleParkingLotService.save(vehicleSyncConverter.toVehicleParkingLot(parkingLotInfoJieShunDTO, projectId));

        log.info("JieShun Sync End!");
    }

    @Override
    protected List<VehicleParkingLotSecondBO> queryParkingLots(Integer projectId, String parkingLotName) {
        return vehicleParkingLotService.queryParkingLots(projectId, parkingLotName);
    }

    @Override
    protected VehicleParkingLotSecondBO queryParkingLot(Integer projectId) {
        return vehicleParkingLotService.queryParkingLot(projectId);
    }

    @Override
    protected VehicleCarportBO getVehicleCarportBOFromHttp(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        CarportJieShunDTO carport = jieShunHttpService.getCarport(vehicleIntegratedConfigBO);
        return vehicleCarportConverter.carportJieShunDTO2VehicleCarportBO(carport);
    }

    @Override
    protected Integer statisticsEnterCount(Integer projectId, String parkId, Date ingestionDate) {
        return vehicleAccessRecordService.accessRecordForCount(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForEnter(projectId, parkId, ingestionDate));
    }

    @Override
    protected Integer statisticsGoOutCount(Integer projectId, String parkId, Date ingestionDate) {
        return vehicleAccessRecordService.accessRecordForCount(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForGoOut(projectId, parkId, ingestionDate));
    }

    @Override
    protected void pushDwsAccessRecordForHourDataToCentBi(Integer projectId, String parkingLotName, Integer totalForCount, Integer enterForCount, Integer goOutForCount, Date processDate, Date ingestionDate) {
        biVehicleService.addDwsAccessRecordForHour(centBiConverter.toCreateBiVehicleAccessRecordHourFeignDTO(projectId, parkingLotName, totalForCount, enterForCount, goOutForCount, processDate, ingestionDate));
    }

    @Override
    protected Integer statisticsTotalPaySum(Integer projectId, String parkId, Date ingestionDate) {
        return vehiclePayRecordService.payRecordForCount(vehiclePayRecordConverter.toQueryVehiclePayRecordForCountBO(projectId, parkId, ingestionDate));
    }

    @Override
    protected void pushDwsPayRecordForHourDataToCentBi(Integer projectId, String parkingLotName, Integer totalPay, Integer webChatPay, Integer aliPay, Integer onlinePay, Integer other, Date processDate, Date ingestionDate) {
        biVehicleService.addDwsPayRecordForHour(centBiConverter.toCreateBiVehiclePayRecordHourFeignDTO(projectId, parkingLotName, totalPay, 0, 0, 0, 0, processDate, ingestionDate));
    }

}
